<template>
    <div>
        <!-- 恢复出厂设置确认弹窗 -->
        <confirmDialog :btnLoading="loading" ref="factoryResetConfirmRef" :title="$t('devicemanagement.factoryreset')" :confirmText="$t('devicemanagement.factoryreset')" @confirm="doFactoryReset">
            <span>{{ this.$t('devicemanagement.factoryresetText', { XXX: resetTime }) }}</span>
        </confirmDialog>

        <!-- 恢复出厂设置倒计时弹窗 -->
        <confirmDialog ref="factoryResetRef" countdownId="restoreDownTime" :title="$t('devicemanagement.factoryresetIng')" :isShowLoading="true" :isShowSuccessMessage="false" :count="resetTime" :isShowFooter="false" @countdownEnd="countdownEnd">
            <div style="width: 100%; text-align: center; font-size: 15px">
                <span>{{ $t('devicemanagement.rebootingText') }}</span>
            </div>
        </confirmDialog>
    </div>
</template>

<script>
import { LOCAL_KEY } from '@/constant/dict';
import { systemOprate } from '@/api/system';

export default {
    name: 'FactoryRest',
    data() {
        return {
            loading: false
        };
    },
    computed: {
        resetTime() {
            const DEFAULT_RESET_TIME = 180;

            return DEFAULT_RESET_TIME;
        }
    },
    methods: {
        // 打开恢复出厂设置确认窗口
        showFactoryResetDialog() {
            this.$refs.factoryResetConfirmRef.show();
        },

        // 恢复出厂设置
        async doFactoryReset() {
            this.loading = true;
            try {
                const res = await systemOprate("reset");
                console.log("doFactoryReset:", res);
                this.$store.state.globalTimerStopFlag = true;
                this.$refs.factoryResetConfirmRef.hide();
                this.$refs.factoryResetRef.show();
                this.$refs.factoryResetRef.startCountdown();
                this.$message.success(this.$t('common.success'));
                sessionStorage.clear();
                localStorage.clear();
            } catch (error) {
                this.$message.error(this.$t('common.error'));
            } 
            this.loading = false;
        },
        // 倒计时结束回调
        countdownEnd() {
            localStorage.removeItem(LOCAL_KEY.IS_AUTH_TOKEN);
            window.location.reload();
        }
    }
};
</script>
<style scoped lang="less">
@import '@/assets/css/theme_blue/component_common.less';
@import '@/assets/css/theme_blue/component_round.less';
</style>
